package net.vsame.url2sql.url;

import java.lang.reflect.Method;

import net.vsame.url2sql.helper.Url2SqlContext;
import net.vsame.url2sql.helper.WebHelper;
import net.vsame.url2sql.url.UrlMapping.Chain;
import net.vsame.url2sql.url.inter.Interceptor;

/**
 * Action 执行拦截器
 * @author <a href="mailto:gaollg@sina.com">Gaollg</a>
 * @data 2013-5-6
 *
 */
public class ActionInterceptor implements Interceptor{
	
	/**
	 * 调用方法
	 * @param method
	 * @return
	 */
	public static Object invokeMethod(Method method){
		try {
			Object o = method.getDeclaringClass().newInstance();
			return method.invoke(o);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public void init(UrlMapping urlMapping) {
	}
	
	@Override
	public void invoke(Chain chain) {
		//调用Action
		Url2SqlContext c =  WebHelper.getContext();
		UrlConfig u = c.getUrlConfig();
		Method m = u.getMethod();
		if(m != null){
			invokeMethod(m);
		}else {
			//TODO 执行JS
			
		}
	}
	
	
	@Override
	public void destroy() {
		
	}
	
}
